package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionData;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.DaFuWengActivity;
import com.yanqu.road.server.manager.cross.battle.DaFuWengDataMgr;
import com.yanqu.road.server.manager.cross.battle.DaFuWengMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = Protocol.C_CROSS_DA_FU_WENG_HANDLE_ZAO_YAO, desc = "处理造谣")
public class CrossHandleZaoYaoCmd extends CrossUnionCommand {

    int activityId;
    long doServerId;
    long doUserId;
    int recordId;
    MonopolyProto.MonopolyPlayerMsg doPlayer;
    int type;
    private String unionUid;
    private String zaoyaoUnionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MonopolyProto.CrossDfwHandleZaoYaoReqMsg reqMsg = MonopolyProto.CrossDfwHandleZaoYaoReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        doServerId = reqMsg.getDoServerId();
        doUserId = reqMsg.getDoUserId();
        recordId = reqMsg.getRecordId();
        doPlayer = reqMsg.getDoPlayer();
        type = reqMsg.getType();
        unionUid = reqMsg.getSelfUnionId();
        if (reqMsg.hasZaoyaoUnionId()) {
            zaoyaoUnionId = reqMsg.getZaoyaoUnionId();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DaFuWengActivity activity = DaFuWengMgr.getDaFuWengActivity(activityId, groupId);
        MonopolyProto.CrossHandleZaoYaoRespMsg.Builder respMsg = MonopolyProto.CrossHandleZaoYaoRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setDoUserId(doUserId);
        respMsg.setType(type);
        UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(doPlayer.getPlayer());
        CrossUserMgr.updateUserBaseInfo(doUserId, userBaseInfo);
        respMsg.setUnionUid("");
        if (activity != null) {
            DFWConfig dfwConfig = DaFuWengDataMgr.getDfwConfig(activityId);
            if (type == 1) {

            }else if (type == 3) {
                List<DFWUnionRecord> recordList = DaFuWengDataMgr.getUnionRecord(activityId, unionUid, 1, groupId);
                DFWUnionRecord record = null;
                for (DFWUnionRecord tmpRecord : recordList) {
                    if (tmpRecord.getRecordId() == recordId) {
                        record = tmpRecord;
                        break;
                    }
                }
                if (record == null) {
                    respMsg.setResult(GameErrorCode.E_ACTIVITY_DFW_PARAM_ERROR);
                    YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_ZAO_YAO_RESULT_FROM_CROSS, respMsg);
                    MessageHelper.sendPacket(doServerId, doUserId, YanQuMessage);
                    return;
                }
                // 判断是不是自身商会
                if (record.getUserBaseInfo().getUnionUid().equals(unionUid)) {
                    respMsg.setResult(GameErrorCode.E_ACTIVITY_DFW_CAN_NOT_SELF_UNION);
                    YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_ZAO_YAO_RESULT_FROM_CROSS, respMsg);
                    MessageHelper.sendPacket(doServerId, doUserId, YanQuMessage);
                    return;
                }

                // 反击次数判断
                int beatBackTimes = record.getBeatBackTimes();
                if (beatBackTimes >= dfwConfig.getUnionBeatBackTimes()) {
                    respMsg.setResult(GameErrorCode.E_ACTIVITY_DFW_ZAO_YAO_TIMES_NO_ENOUGH);
                    YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_ZAO_YAO_RESULT_FROM_CROSS, respMsg);
                    MessageHelper.sendPacket(doServerId, doUserId, YanQuMessage);
                    return;
                }

                zaoyaoUnionId = record.getUserBaseInfo().getUnionUid();
            }
            boolean isFirst = true;
            for (Long sid : dfwConfig.getActivityInfo().getServerIdList()) {
                MonopolyProto.ZaoYaoFromCrossReqMsg.Builder builder = MonopolyProto.ZaoYaoFromCrossReqMsg.newBuilder();
                if (isFirst) {
                    builder.setZaoyaoServerId(sid);
                    isFirst = false;
                }
                builder.setActivityId(activityId);
                builder.setDoServerId(doServerId);
                builder.setDoUserId(doUserId);
                builder.setUnionUid(zaoyaoUnionId);
                builder.setDoPlayer(doPlayer);
                builder.setType(type);
                builder.setRecordId(recordId);

                YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_ZAO_YAO_FROM_CROSS, builder);
                MessageHelper.sendPacket(sid, 0, YanQuMessage);
            }
        }
    }

    @Override
    public void sendData() {

    }
}
